package cn.dlc.com.service;

import cn.dlc.com.entity.BaseDataArea;
import cn.dlc.com.mapper.BaseDataAreaMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 区域编码表
 * @author： tanzy
 * @date：   2021-03-30
 * @version： V1.0
 */
@Service
public class BaseDataAreaServiceImpl extends ServiceImpl<BaseDataAreaMapper, BaseDataArea> implements IBaseDataAreaService {



    @Override
    public List<BaseDataArea> getProvinceList() {
        /*String parentCode = "000000";
        LambdaQueryWrapper<BaseDataArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseDataArea::getParentCode, parentCode);

        provinceList = this.list(queryWrapper);*/
        return findByParentCode("000000");
    }

    @Override
    public List<BaseDataArea> findByParentCode(String parentCode) {
        LambdaQueryWrapper<BaseDataArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseDataArea::getParentCode, parentCode);
        return this.list(queryWrapper);
    }

    @Override
    public List<BaseDataArea> findAll() {

        List<BaseDataArea> byParentCode = this.findByParentCode("000000");
        if(byParentCode != null && byParentCode.size() > 0) {
            for (BaseDataArea baseDataArea : byParentCode) {
                baseDataArea.setAreaList(this.findSub(baseDataArea.getAreaCode()));
            }
        }

        return byParentCode;
    }

    private List<BaseDataArea> findSub(String parentCode) {
        List<BaseDataArea> byParentCode = this.findByParentCode(parentCode);
        if(byParentCode == null ||(byParentCode != null &&  byParentCode.size() == 0)){
            return null;
        } else {
            for (BaseDataArea baseDataArea : byParentCode) {
                baseDataArea.setAreaList(this.findSub(baseDataArea.getAreaCode()));
            }
            return byParentCode;
        }
    }

}
